package com.zw.pdm.module.production.controller;

import com.zw.pdm.base.BaseController;
import com.zw.pdm.common.JsonResult;
import com.zw.pdm.entity.production.MakeSheet;
import com.zw.pdm.entity.rep.Change;
import com.zw.pdm.entity.rep.VerifyProcess;
import com.zw.pdm.module.basic.service.DictCommonService;
import com.zw.pdm.module.production.model.MakeSheetModel;
import com.zw.pdm.module.production.service.ProductionCheckService;
import com.zw.pdm.module.production.service.ProductionSheetService;
import com.zw.pdm.module.report.service.ReportCheckService;
import com.zw.pdm.module.report.service.SheetService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.zw.pdm.constant.Constant.GROUP_CHANGE_REASON;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName ProductionCheckController.java
 * @Description TODO 制作件清单审核
 * @createTime 2020年10月06日 16:43:00
 */
@Controller
@RequestMapping("/production/check")
@SuppressWarnings("all")
public class ProductionCheckController extends BaseController {


    @Autowired
    private ProductionCheckService productionCheckService;
    @Autowired
    private ProductionSheetService productionSheetService;


    @Autowired
    private ReportCheckService reportCheckService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;

    @GetMapping
    @RequiresPermissions({"production:check:view"})
    public String page(Model model) {
        // 查部级审核人，用于修改审核人功能
        model.addAttribute("users", reportCheckService.findVerifier());
        model.addAttribute("userers", reportCheckService.findInformant());
        return "production/check/makeCheck";
    }

    /** 制作件审核列表 */
    @ResponseBody
    @PostMapping("/list")
    @RequiresPermissions({"production:check:view"})
    public JsonResult sheetList(MakeSheetModel makeSheetmodel) {
        startPage();
        List<MakeSheetModel> list = productionCheckService.findWithReportCheck(makeSheetmodel);
        return jsonResult(list);
    }

    /** 制作件查看页面 */
    @GetMapping("/info/{id}")
    @RequiresPermissions({"production:check:view"})
    public String info(@PathVariable("id") Integer sheetId, Model model) {
        MakeSheet makeSheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("MakeSheet", makeSheet);
        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
        if (makeSheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/list/makePrint";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            return "production/list/modifyMakeChangePrint";
        }
    }

    /** 制作件审核页面 */
//    @GetMapping("/{id}")
//    @RequiresPermissions({"production:check:view"})
//    public String verify(@PathVariable("id") Integer sheetId, Model model) {
//        MakeSheet sheet = productionSheetService.getSheetById(sheetId);
//        model.addAttribute("makeSheet", sheet);
//        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
//        if (sheet.getSheetType() == 1) {
//            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
//            return "production/check/makeVerify";
//        } else {
//            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
//            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
//            return "production/check/makeChangeVerify";
//        }
//    }


    /** 审核页面的同意按钮 */
    @ResponseBody
    @PostMapping("/success")
    @RequiresPermissions({"production:check:edit"})
    public JsonResult success(Integer id) {
        return jsonResult(productionCheckService.makeVerify(id));
    }

    /** 驳回制作件清单计划 */
    @ResponseBody
    @PostMapping("/reject")
    @RequiresPermissions({"production:check:edit"})
    public JsonResult reject(MakeSheet sheet) {
        return jsonResult(productionCheckService.makeReject(sheet));
    }

    /** 修改审核人 */
    @ResponseBody
    @PostMapping("/edit")
    @RequiresPermissions({"production:check:editVerify"})
    public JsonResult editVerify(VerifyProcess verifyProcess) {
        return isNotNull(reportCheckService.editVerify(verifyProcess));
    }


    /** 修改提报人 */
    @ResponseBody
    @PostMapping("/edit/inforer")
    @RequiresPermissions({"production:edit:inforer"})
    public JsonResult editInforer(Integer postUserId,Integer id) {

        int i = productionCheckService.editInforer(postUserId,id);

        return jsonResult(i);
    }

    /** 取消变更页面 */
    @GetMapping("/cancel/{id}")
    @RequiresPermissions({"production:check:edit"})
    public String cancelPage(@PathVariable("id") Integer sheetId, Model model) {
        model.addAttribute("sheet", sheetService.getSheetById(sheetId));
        return "report/check/cancelPage";
    }

    /** 变更计划列表 */
    @ResponseBody
    @PostMapping("/change/list")
    @RequiresPermissions({"production:check:edit"})
    public JsonResult planList(Change change) {
        startPage();
        List<Change> list = reportCheckService.findChangeDetail(change);
        return jsonResult(list);
    }

    /** 取消变更 */
    @ResponseBody
    @PostMapping("/cancel")
    @RequiresPermissions({"production:check:edit"})
    public JsonResult cancelChange(Integer id) {
        return jsonResult(reportCheckService.cancelChange(id));
    }

    /********************************************************需求变更以后的代码*************************************************************/


    /** 制作件审核页面 */
    @GetMapping("/{id}")
    @RequiresPermissions({"production:check:view"})
    public String modifyVerify(@PathVariable("id") Integer sheetId, Model model) {
        MakeSheet sheet = productionSheetService.getSheetById(sheetId);
        model.addAttribute("makeSheet", sheet);
        model.addAttribute("process", productionSheetService.findVpBySheetId(sheetId));
        if (sheet.getSheetType() == 1) {
            model.addAttribute("makePlans", productionSheetService.findPlanBySheetId(sheetId));
            return "production/check/makeVerify";
        } else {
            model.addAttribute("reasons", dictCommonService.findDictByGroupName(GROUP_CHANGE_REASON));
            model.addAttribute("changes", productionSheetService.findMakeResultChangeBySheetId(sheetId));
            return "production/check/modifyMakeChangeVerify";
        }
    }


}
